package com.zyw.service.personnel.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.PersonnelDepartment;
import com.zyw.from.personnelFrom;
import com.zyw.mapper.personnel.PersonnelDepartmentMapper;
import com.zyw.service.personnel.PersonnelDepartmentService;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PersonnelDepartmentServiceImpl implements PersonnelDepartmentService {

    @Resource
    public PersonnelDepartmentMapper personnelDepartmentMapper;

    @Override
    public List<PersonnelDepartment> selectAll() {
        return personnelDepartmentMapper.selectAll();
    }

    @Override
    public LayUITableVO<List<PersonnelDepartment>> findPage(Map<String, Object> map) {
        personnelFrom from = (personnelFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(PersonnelDepartment.class);
        Example.Criteria c = example.createCriteria();

        if (! "".equals(from.getDepartmentName()) && null!=from.getDepartmentName()){
            c.andLike("name","%"+from.getDepartmentName()+"%");
        }

        List<PersonnelDepartment> list = personnelDepartmentMapper.selectByExample(example);

        PageInfo<PersonnelDepartment> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<PersonnelDepartment>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public PersonnelDepartment selectById(String id) {
        return personnelDepartmentMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(PersonnelDepartment personnelDepartment) {
        personnelDepartmentMapper.updateByPrimaryKeySelective(personnelDepartment);

    }

    @Override
    @Transactional
    public void save(PersonnelDepartment personnelDepartment) {
        personnelDepartmentMapper.insertSelective(personnelDepartment);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        personnelDepartmentMapper.deleteByPrimaryKey(id);
    }
}
